﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using brockredge.CLASSES;
namespace brockredge
{
    public partial class Addvertisment_by_user : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["userId"] == null)
            {
                Response.Redirect("/Default.aspx");
            }
            if (Page.IsPostBack)
            {
                return;
            }
            var qstr = Request.QueryString["u"];
            if (qstr==null)
            {
                DisplayAllAdd();
                
            }
            else if(qstr.Equals("T"))
            {
                DisplayAddByUser(Session["userId"].ToString());
            }
        }

        private void DisplayAddByUser(string user_id)
        {
            testDataContext tdc = new testDataContext();
            var addvertisementQ = from add in tdc.Advertisements
                                  where tdc.User_Advertisements.Any(ua => ua.add_id == add.add_id && ua.user_id == (Guid)user_id.ToGuid())
                                  orderby add.createDate descending
                                  select add;

            if (addvertisementQ.Count() > 0)
            {
                addbyUserrep.DataSource = addvertisementQ;
                addbyUserrep.DataBind();
            }

        }

        private void DisplayAllAdd()
        {
            testDataContext tdc = new testDataContext();
            var addvertisementQ = from add in tdc.Advertisements
                                  orderby add.createDate descending
                                  select add;

            if (addvertisementQ.Count() > 0)
            {
                addbyUserrep.DataSource = addvertisementQ;
                addbyUserrep.DataBind();
            }
        }
    }

}